﻿/*****************************************************************
版本         创建/修改人        时间                描述/修改内容
====         =========       ========         ==================================
1.0           张晓松          2020-04-24       初始版本

******************************************************************/
using Kernel.Command.Core.Attachment;
using Kernel.Model.Core.Attachment;

namespace WebAPI.Areas.Core.Controllers;

[ApiVersion("1.0")]
//[Authorize]
public class FileController : CoreBaseController
{
    private IMediator mediator;

    public FileController(IMediator mediator)
    {
        this.mediator = mediator;
    }

    /// <summary>
    /// 打包下载文件
    /// </summary>
    /// <remarks>
    /// GET /api/v1/Core/File/zip/123
    /// </remarks>
    /// <param name="bizID">业务ID</param>
    /// <returns>返回打包文件路径</returns>
    /// <response code="400">错误请求</response> 
    [HttpGet]
    [Route("zip/{bizID}"), MapToApiVersion("1.0")]
    public async Task<IActionResult> DownloadZip_V1_0(string bizID)
    {
        var result = await mediator.Send(new DownloadZipCommand { BizId = bizID });
        return Ok(result);
    }

    /// <summary>
    /// 下载文件
    /// </summary>
    [HttpGet]
    [Route("download/{attachID}"), MapToApiVersion("1.0")]
    public async Task<IActionResult> DownloadFile_V1_0(string attachID)
    {
        var result = await mediator.Send(new DownFileCommand { AttachID = attachID });

        if (result == null)
            throw new KernelException("文件不存在！");
        else
            return File(result.Data.Content, result.Data.ContentType, result.Data.FileName);
    }

    /// <summary>
    /// 获取文件信息
    /// </summary>
    [HttpGet]
    [Route("files/{bizID}"), MapToApiVersion("1.0")]
    [Caching(ExpiresSlidingSeconds = 10)]
    [TimeOut(TimeoutSeconds = 5)]
    public virtual async Task<IActionResult> GetFiles_V1_0(string bizID)
    {
        var result = await mediator.Send(new GetFilesCommand { BizId = bizID });
        return Ok(result);
    }

    /// <summary>
    /// 获取文件信息
    /// </summary>
    [HttpGet]
    [Route("file/{attachID}"), MapToApiVersion("1.0")]
    public async Task<IActionResult> GetFile_V1_0(string attachID)
    {
        var result = await mediator.Send(new GetFileCommand { AttachId = attachID });
        return Ok(result);
    }

    /// <summary>
    /// 上传文件
    /// </summary>
    [HttpPost]
    [Route("file"), MapToApiVersion("1.0")]
    public async Task<IActionResult> UploadFiles_V1_0(string bizID, IFormFile formFile)
    {
        var result = await mediator.Send(new UploadFilesCommand
        {
            BizId = bizID,
            Files = Request.Form.Files
        });
        return Ok(result);

    }

    /// <summary>
    /// 删除文件，按业务ID删除
    /// </summary>
    [HttpDelete]
    [Route("file/{bizID}"), MapToApiVersion("1.0")]
    public async Task<IActionResult> DeleteFilesByBizID_V1_0(string bizID)
    {
        var result = await mediator.Send(new DeleteFilesByBizIDCommand { BizId = bizID });
        return Ok(result);
    }

    /// <summary>
    /// 删除文件，按文件ID删除（多个ID以逗号分隔）
    /// </summary>
    [HttpDelete]
    [Route("file"), MapToApiVersion("1.0")]
    public async Task<IActionResult> DeleteFilesByAttachIDs_V1_0([FromBody]SysAttachmentsInParams inPara)
    {
        var result = await mediator.Send(new DeleteFilesCommand { AttachIDs = inPara.AttachIDs });
        return Ok(result);
    }

}
